package com.mux.comms;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

/**
 * 日期操作类
 * @author devin.chen
 *
 */
public class DateUtil {
	
	/**
	 * 获取当前的时间   
	 * @param zoneid 时区
	 * @return 
	 */
	public static LocalDateTime getCurrentLocalDateTime(String zoneid){
		if(StringUtils.isNotBlank(zoneid)){
			return LocalDateTime.now(ZoneId.of(zoneid));
		}
		
		LocalDateTime now = LocalDateTime.now();
		return now;
	}
	
	
	/**
	 * 获取当前时间 字符串
	 * @param zoneid     时区
	 * @param formatStr  日期格式
	 * @return
	 */
	public static String getCurrentDateTime(String zoneid,String formatStr){
		LocalDateTime dateTime = getCurrentLocalDateTime(zoneid);
		return getLocalDateTimeToString(dateTime, formatStr);
	}
	
	
	/**
	 * 将LocalDateTime 转为 String
	 * @param dateTime  时间
	 * @param formatStr  格式
	 * @return
	 */
	public static String getLocalDateTimeToString(LocalDateTime dateTime,String formatStr){
		DateTimeFormatter format = DateTimeFormatter.ofPattern(formatStr); 
		String landing = dateTime.format(format);
		return landing;
	}
	
	/**
	 * 将LocalDate 转为String 
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static String getLocalDateToString(LocalDate date,String formatStr){
		DateTimeFormatter format = DateTimeFormatter.ofPattern(formatStr); 
		String landing = date.format(format);
		return landing;
	}
	
	
	/**
	 * 日期字符串 转为 LocalDate
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static LocalDate stringToLocalDate(String date,String formatStr){
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatStr);
		return LocalDate.parse(date,formatter);
	}
	
	
	/**
	 * 获取  n天前的 时间
	 * @param days   天数
	 * @return
	 */
	public static LocalDateTime getMinusDayDateTime(LocalDateTime dateTime,long days){
		return dateTime.minusDays(days);
	}
	
	
	/**
	 * 格式化日期 
	 * @param date  
	 * @param formatStr 格式串
	 * @return
	 */
	public static String formatDate(Date date,String formatStr){
		SimpleDateFormat dff = new SimpleDateFormat(Constants.DATE_FORMAT);
		return  dff.format(date);
	}
	
	
	/**
	 * String 转为 Calendar
	 * @param date
	 * @param fmt
	 * @return
	 * @throws Exception
	 */
	public static Calendar stringDateToCalendar(String date,String fmt) throws Exception{
		SimpleDateFormat sdf= new SimpleDateFormat(fmt);
		Date calDate = sdf.parse(date);
		Calendar startCal = Calendar.getInstance();
		startCal.setTime(calDate);
		return startCal;
	}
	
	/**
	 * Calendar 转 String
	 * @param cal
	 * @param fmt
	 * @return
	 */
	public static String calendarToStringDate(Calendar cal,String fmt){
		SimpleDateFormat sdf = new SimpleDateFormat(fmt);
		return sdf.format(cal.getTime());
	}
	
	
	/**
	 * 对Calendar 增加  n 天 
	 * @param cal
	 * @param days
	 * @return
	 */
	public static Calendar getAddCalendar(Calendar cal,int days){
		 cal.add(Calendar.DATE,days);
		 return cal;
	}
	
	
	public static void main(String[] args) {
		String sql = "20161210";
	//	System.out.println(stringToLocalDate(sql,"yyyy-MM-dd"));
		System.out.println(stringToLocalDate(sql,"yyyyMMdd"));
	}
	

}
